package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.laihui.Constants;
import com.laihui.MyApplication;
import com.laihui.base.BaseActivity;
import com.laihui.entity.Account;
import com.laihui.entity.MemberBank;
import com.laihui.entity.RetMap;
import com.laihui.utils.ToastUtil;
import com.laihui.view.TopBar;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 提现页面
 */
public class WithDrawActivity extends BaseActivity {

    @BindView(R.id.topbar)//topbar
            TopBar topbar;
    @BindView(R.id.iv_bank_logo)//银行卡logo
            ImageView ivBankLogo;
    @BindView(R.id.tv_bank_card)//银行卡
            TextView tvBankCard;
    @BindView(R.id.et_with_draw_money)//提现金额
            EditText etWithDrawMoney;
    @BindView(R.id.et_password)//交易密码
            EditText etPassword;
    @BindView(R.id.btn_submit)//提交按钮
            Button btnSubmit;
    @BindView(R.id.tv_bank_type)//银行卡名字
            TextView tvBankType;
    @BindView(R.id.tv_with_draw_money)
    TextView tvWithDrawMoney;

    @BindView(R.id.tv_help)
    TextView tv_help;


    private Activity mActivity;
    private MyApplication application;
    private DecimalFormat df = new DecimalFormat("0.00");

    @Override
    public void init() {
        mActivity = WithDrawActivity.this;
        application = (MyApplication) getApplication();
        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
        bankcard();
        account();
    }

    private void account() {
        Map<String, String> map = new HashMap<>();
        map.put("member_id", application.userId);
        application.apiService.getAccount(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Account>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Account account) {
                        tvWithDrawMoney.setText(df.format(account.item.balance));
//                        tvNoWithDrawMoney.setText(df.format(Math.abs(toWithDraw.getAccount().getNo_use_money())));
                    }
                });
    }

    private void bankcard() {

        Map<String, String> map = new HashMap<>();
        map.put("member_id", application.userId);
        application.apiService.isBankBind(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MemberBank>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(MemberBank memberBank) {
                        tvBankType.setText(memberBank.item.bank);
                        Glide.with(mActivity).load(Constants.BASE_URL+memberBank.item.bank_img_mobile).into(ivBankLogo);

                        //tvBankNumber.setText(bankCard.getBankCard().getBank_cardno());
                        if (memberBank.item.bank_cardno.length()==16){
                            tvBankCard.setText("**************"+memberBank.item.bank_cardno.substring(12,16));
                        }else{
                            tvBankCard.setText("**************"+memberBank.item.bank_cardno.substring(15,19));
                        }

                    }
                });


    }

    private void withDraw() {
        final String money = etWithDrawMoney.getText().toString();
        String password = etPassword.getText().toString();
        if (money.isEmpty()) {
            ToastUtil.showToast(mActivity, "请输入提现金额");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("member_id", application.userId);
        map.put("money", money);
        map.put("paypassword", password);

        application.apiService.doCash(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RetMap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RetMap retMap) {
                        if (retMap.status.equals("1")) {
                            Intent intent = new Intent(WithDrawActivity.this,WithDrawSuccesActivity.class);
                            intent.putExtra("bank",tvBankType.getText().toString());
                            intent.putExtra("lastname",tvBankCard.getText());
                            intent.putExtra("money",money+"");
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtil.showToast(mActivity, retMap.message);
                        }
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_with_draw;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }

    @OnClick({R.id.btn_submit,R.id.tv_help})
    public void onClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                withDraw();
                break;

            case R.id.tv_help:
                Intent intent = new Intent(WithDrawActivity.this,ActivityHelp.class);
                startActivity(intent);
                break;
        }



    }

}
